﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Game_Fly.Code;
using System.IO;
using Game_Fly.GameSound;

namespace Game_Fly
{
    public partial class GameUI : Form
    {
        //游戏全局
        public static int elapsedFrames = 0;//累计帧数
        public static Timer Time_Run;//游戏跑起的总线程
        public static BackGround GameBackGround;//背景
        public static Plane GamePlane;//战机
        public static List<Bullet> ListBullets;//子弹
        public static List<BulletTwo> ListBulletsTwo;//升级子弹
        public static List<Enemy1> ListEnemy1;//敌机1
        public static List<Enemy2> ListEnemy2;//敌机2
        public static List<EnemyBoss> ListEnemyBoss;//敌机Boss
        public static List<BulletTwoPrize> ListBulletTwoPrize;//奖励子弹
        public static List<LifePrize> ListLifePrize;//奖励生命
        public static GameUI GameForm;
        /// <summary>
        /// 游戏初始化
        /// </summary>
        public GameUI()
        {
            //采用双缓冲，解决角色移动屏幕闪烁
            //this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer, true);
            InitializeComponent();
            //游戏组件初始化
            Time_Run = Time_Game;//总线程
            GameForm = this;
            GameBackGround = new BackGround();
            GamePlane = new Plane();
            ListBullets = new List<Bullet>();
            ListEnemy1 = new List<Enemy1>();
            ListEnemy2 = new List<Enemy2>();
            ListEnemyBoss = new List<EnemyBoss>();
            ListBulletsTwo = new List<BulletTwo>();
            ListBulletTwoPrize = new List<BulletTwoPrize>();
            ListLifePrize = new List<LifePrize>();

            #region 随机播放音乐
            //随机产生游戏音乐
            Random randmon = new Random();
            List<Stream> list_Music = new List<Stream>()
            {
                GameMusic.Bj_Music1,
                GameMusic.Bj_Music2,
                GameMusic.Bj_Music3
            };
            //游戏效果音乐启动
            GameSound.GameSound.BackSoundPlay(list_Music[randmon.Next(0, list_Music.Count)]);
            #endregion
        }

        /// <summary>
        /// 绘制图形
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            //获得绘画设备（画笔，画板）
            Graphics g = e.Graphics;
            //绘制图像-游戏背景
            GameBackGround.Draw(g);
            //绘制图像-游戏战机
            GamePlane.Draw(g);
            //绘制泛型中的子弹
            foreach (Bullet item in ListBullets)
                item.Draw(g);
            //绘制泛型中的子弹2
            foreach (BulletTwo item in ListBulletsTwo)
                item.Draw(g);
            //敌机1
            foreach (Enemy1 item in ListEnemy1)
                item.Draw(g);
            //敌机2
            foreach (Enemy2 item in ListEnemy2)
                item.Draw(g);
            //敌机Boss
            foreach (EnemyBoss item in ListEnemyBoss)
                item.Draw(g);
            //子弹奖品
            foreach (BulletTwoPrize item in ListBulletTwoPrize)
                item.Draw(g);
            //生命奖励
            foreach (LifePrize item in ListLifePrize)
                item.Draw(g);
            //游戏是否结束
            if (GamePlane.IsOver)
            {
                GamePlane.Life = 0;
                g.DrawString("等级：" + GameUI.GamePlane.Grade + "\n分数：" + GameUI.GamePlane.HitNumber + "\nGameOver 重玩[Enter]?", new Font("微软雅黑", 20), Brushes.Red, new PointF((GameUI.GameForm.ClientSize.Width - GameUI.GameBackGround.Width) / 2 + GameUI.GameBackGround.Width / 3, GameUI.GameForm.ClientSize.Height / 2));
                GameUI.Time_Run.Stop();
            }
        }

        /// <summary>
        /// 修改某一帧的数据
        /// </summary>
        /// <param name="elapsedFrames">计数帧</param>
        private void Update(int elapsedFrames)
        {
            //修改数据-游戏背景往下移动
            GameBackGround.Update(elapsedFrames);
            //修改数据-修改战机数据
            GamePlane.Update(elapsedFrames);
            //修改数据-修改所有子弹数据
            for (int i = ListBullets.Count - 1; i >= 0; i--)
            {
                ListBullets[i].Update(elapsedFrames);
            }
            //修改数据-修改所有子弹2数据
            for (int i = ListBulletsTwo.Count - 1; i >= 0; i--)
            {
                ListBulletsTwo[i].Update(elapsedFrames);
            }
            //修改数据-修改所有敌机1的数据
            for (int i = ListEnemy1.Count - 1; i >= 0; i--)
            {
                ListEnemy1[i].Update(elapsedFrames);
            }
            //修改数据-修改所有敌机Boss的数据
            for (int i = ListEnemy2.Count - 1; i >= 0; i--)
            {
                ListEnemy2[i].Update(elapsedFrames);
            }
            //修改数据-修改所有敌机2的数据
            for (int i = ListEnemyBoss.Count - 1; i >= 0; i--)
            {
                ListEnemyBoss[i].Update(elapsedFrames);
            }
            //修改数据-修改所有子弹奖励的数据
            for (int i = ListBulletTwoPrize.Count - 1; i >= 0; i--)
            {
                ListBulletTwoPrize[i].Update(elapsedFrames);
            }
            //修改数据-修改所有生命奖励的数据
            for (int i = ListLifePrize.Count - 1; i >= 0; i--)
            {
                ListLifePrize[i].Update(elapsedFrames);
            }
        }

        private void Time_Game_Tick(object sender, EventArgs e)
        {
            //累计帧数
            elapsedFrames++;
            //修改第几帧数据
            Update(elapsedFrames);
            //刷屏重绘
            //this.Refresh();
            //重绘窗体，只重新绘制变化的部分
            this.Invalidate(new Rectangle(0, 0, this.Width, this.Height), true);
        }
    }
}
